#!/usr/bin/env sh

cd ~/app-root/data/temp/hosts/

get_hn_seo() {
    curl -s http://vpn.hn-seo.com/ |grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}">>ip.txt
}
get_sslproxies() {
	curl -s https://www.sslproxies.org/|grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}">>ip.txt
}

get_samair() {
	result=$(curl -s http://www.samair.ru/proxy/proxy-01.htm)
	pages=$(echo "$result" | grep -o "proxy\-[0-9]\{2\}\.htm" | sort -u )
	echo "$result" |grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}">>ip.txt
	for i in $pages;do
	  result=$(curl -s http://www.samair.ru/proxy/$i)
	  echo "$result" |grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}">>ip.txt
	done
}

get_incloak() {
	pages=$(curl -s https://incloak.com/proxy-list/ |grep -o '/proxy-list/[a-z0-9\?\=]*#list' |sed '1d')
	for i in $pages;do
	  curl -s https://incloak.com$i |grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" >> ip.txt
	done
}

get_cool_proxy() {
	base='https://www.cool-proxy.net/proxies/http_proxy_list/sort:score/direction:desc'
	pages=$(curl -s $base |grep current |grep -o "[0-9]*" |tail -1)
	for ((i=1;i<=$pages;i++)); do
	  result=$(curl -s $base/page:$i |grep -o "Base64.decode.*" |awk -F '\"' '{print $2}')
	  for n in $result; do
	    #rot13
	    echo "$n" | tr '[A-Za-z]' '[N-ZA-Mn-za-m]'|base64 -d -i |sed 's#$#\n#' >> ip.txt
	  done
	done
}

get_proxy_list_org() {
	base='https://proxy-list.org/english/index.php'
	pages=$(curl -s $base | sed -n '/active/,/next/p' |grep -o '[0-9]*'|sort -n -u)
	for i in $pages; do
	  result=$(curl -s $base?p=$i |grep -o "Proxy('.*" |awk -F "'" '{print $2}')
	  for n in $result; do
	    echo "$n" |base64 -d -i |sed 's#:.*$#\n#' >> ip.txt
	  done
	done
}

get_proxy_moo_jp() {
  base='http://proxy.moo.jp/'
  pages=$(curl -s $base -H "Accept-Language: en-US;q=0.5,en;q=0.3" | grep 'page=[0-9]*' |sed 's#Next.*##' |grep -o '[0-9]*'|sort -n -u)
  for i in $pages; do
    result=$(curl -s $base?p=$i -H "Accept-Language: en-US;q=0.5,en;q=0.3" |grep -o "IPDecode(\"%[%a-z0-9]*" |awk -F '\"' '{print $2}')
    #urlencode
    echo "$result" | awk 'BEGIN{for(i=0;i<10;i++)hex[i]=i;hex["A"]=hex["a"]=10;hex["B"]=hex["b"]=11;hex["C"]=hex["c"]=12;hex["D"]=hex["d"]=13;hex["E"]=hex["e"]=14;hex["F"]=hex["f"]=15;}{gsub(/\+/," ");i=$0;while(match(i,/%../)){;if(RSTART>1);printf"%s",substr(i,1,RSTART-1);printf"%c",hex[substr(i,RSTART+1,1)]*16+hex[substr(i,RSTART+2,1)];i=substr(i,RSTART+RLENGTH);}print i;}' >> ip.txt
  done
}

get_xicidaili_com() {
  for ((i=1;i<=3;i++)); do
    curl -s http://www.xicidaili.com/wn/$i |grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" >> ip.txt
    curl -s http://www.xicidaili.com/wt/$i |grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" >> ip.txt
    curl -s http://www.kuaidaili.com/free/outha/$i |grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" >> ip.txt
    curl -s http://www.kuaidaili.com/free/outtr/$i |grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" >> ip.txt
  done
}

test_IP() {
  List=$(cat ip.txt)
  for i in $List; do
    if grep -q "^$i$" chinaip.txt; then
        sed -i "/^$i$/d" ip.txt
        exit
    fi
    if curl -s http://ip.taobao.com/service/getIpInfo.php?ip=$i | grep -q 'CN' >/dev/null; then
        sed -i "/^$i$/d" ip.txt
        echo "$i" >> chinaip.txt
    fi
  done
}

get_IP() {
  echo >ip.txt
  get_hn_seo
  get_incloak
  get_sslproxies
  get_cool_proxy
  get_samair
  get_proxy_list_org
  get_proxy_moo_jp
  get_xicidaili_com
  sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n -u ip.txt -o ip.txt
  sed -i '/^$/d' ip.txt
  #test_IP
  mv -f ip.txt ~/app-root/runtime/repo/hosts/ip.txt
}

get_IP
